package rs.etf.mips.crossroad.resources;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JLabel;
import rs.etf.mips.crossroad.logic.Light;

/**
 * Gets internal resources.
 */
public class ResourceManager {
	private static ResourceManager instance;

	public static ResourceManager getInstance() {
		if (instance == null)
			instance = new ResourceManager();
		return instance;
	}

	private ResourceManager() {
		// TODO
	}

	public void setSemaphore(Light light, JLabel label) {
		if (light == Light.Green) {
			URL imageURL = getClass()
					.getResource(
							"/rs/etf/mips/crossroad/resources/Images/Semaphores/traffic-semaphore-green-light-md.png");
			BufferedImage buffImg = null;
			try {
				buffImg = ImageIO.read(imageURL);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			label.setIcon(new javax.swing.ImageIcon(buffImg.getScaledInstance(
					label.getWidth(), label.getHeight(), Image.SCALE_FAST))); // NOI18N
		}
		if (light == Light.Yellow) {
			URL imageURL = getClass()
					.getResource(
							"/rs/etf/mips/crossroad/resources/Images/Semaphores/traffic-semaphore-yellow-light-md.png");
			BufferedImage buffImg = null;
			try {
				buffImg = ImageIO.read(imageURL);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			label.setIcon(new javax.swing.ImageIcon(buffImg.getScaledInstance(
					label.getWidth(), label.getHeight(), Image.SCALE_FAST))); // NOI18N
		}
		if (light == Light.Red) {
			URL imageURL = getClass()
					.getResource(
							"/rs/etf/mips/crossroad/resources/Images/Semaphores/traffic-semaphore-red-light-md.png");
			BufferedImage buffImg = null;
			try {
				buffImg = ImageIO.read(imageURL);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			label.setIcon(new javax.swing.ImageIcon(buffImg.getScaledInstance(
					label.getWidth(), label.getHeight(), Image.SCALE_FAST))); // NOI18N
		}
		if (light == Light.Off) {
			URL imageURL = getClass()
					.getResource(
							"/rs/etf/mips/crossroad/resources/Images/Semaphores/traffic-semaphore-silhouette-md.png");
			BufferedImage buffImg = null;
			try {
				buffImg = ImageIO.read(imageURL);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			label.setIcon(new javax.swing.ImageIcon(buffImg.getScaledInstance(
					label.getWidth(), label.getHeight(), Image.SCALE_FAST))); // NOI18N
		}
	}

	
	public void setPedestrianSemaphore(Light light, JLabel label) {
		if (light == Light.Green) {
			URL imageURL = getClass()
					.getResource(
							"/rs/etf/mips/crossroad/resources/Images/ped_green.png");
			BufferedImage buffImg = null;
			try {
				buffImg = ImageIO.read(imageURL);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			label.setIcon(new javax.swing.ImageIcon(buffImg.getScaledInstance(
					label.getWidth(), label.getHeight(), Image.SCALE_FAST))); // NOI18N
		}
		if (light == Light.Red) {
			URL imageURL = getClass()
					.getResource(
							"/rs/etf/mips/crossroad/resources/Images/ped_red.png");
			BufferedImage buffImg = null;
			try {
				buffImg = ImageIO.read(imageURL);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			label.setIcon(new javax.swing.ImageIcon(buffImg.getScaledInstance(
					label.getWidth(), label.getHeight(), Image.SCALE_FAST))); // NOI18N
		}
		if (light == Light.Off) {
			URL imageURL = getClass()
					.getResource(
							"/rs/etf/mips/crossroad/resources/Images/ped_green.png");
			BufferedImage buffImg = null;
			try {
				buffImg = ImageIO.read(imageURL);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			label.setIcon(new javax.swing.ImageIcon(buffImg.getScaledInstance(
					label.getWidth(), label.getHeight(), Image.SCALE_FAST))); // NOI18N
		}
	}
	
	public void setDisplaySemaphore(int number, Light light, JLabel label) {
		String resource1 = "/rs/etf/mips/crossroad/resources/Images/SevenSegmDispl/64px-Seven_segment_display_";
		String color = "";
		String digit = number+"";
		String resource2 = "_digit_(";
		String resource3 = ").svg.png";
		
		if (light == Light.Green) {
			color = "green";
		}
		if (light == Light.Red) {
			color = "red";
		}
		if (light == Light.Yellow) {
			color = "yellow";
		}

		String resource = resource1 + digit + resource2 + color + resource3;
		
		URL imageURL = getClass()
				.getResource(resource);
		BufferedImage buffImg = null;
		try {
			buffImg = ImageIO.read(imageURL);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		label.setIcon(new javax.swing.ImageIcon(buffImg.getScaledInstance(
				label.getWidth(), label.getHeight(), Image.SCALE_FAST))); // NOI18N
	}
}
